//
//  assetsViewController.m
//  DBAChain
//
//  Created by Mxionlly on 2018/9/6.
//  Copyright © 2018年 Evan. All rights reserved.
//

#import "assetsViewController.h"
#import "AssetsHeadView.h"
#import "AssetsTopView.h"
#import "AssetsTableViewCell.h"
#import "CommonData.h"
#import "AssetsListTableViewCell.h"
#import "SuanMaViewController.h"
#import "QianBaoViewController.h"
#import "QianBaoTiViewController.h"
#import "MeSetViewController.h"
#import "dataOverviewInfo.h"
#import "taskOverviewService.h"
#import "taskUserManagerService.h"
#import "MeSourceDetailViewController.h"
#import "AssetsOrderListViewController.h"

@interface assetsViewController ()<UITableViewDelegate,UITableViewDataSource,assetsScrollViewDelegate>
@property (nonatomic,strong)    UITableView *tableView;
@property (nonatomic,strong)    AssetsHeadView *headView;
@property (nonatomic,strong)    AssetsTopView  *topView;
@property (nonatomic,strong)    NSArray        *cellArray;
///
@property (nonatomic,strong)dataOverviewInfo *gteInfo;
@property (nonatomic,strong)dataOverviewInfo *gtkInfo;

///当前类型是否是GTE
@property (nonatomic,assign)    BOOL  isGTE;
@end

@implementation assetsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.view.backgroundColor = [UIColor colorWithRed:25/255.0 green:30/255.0 blue:46/255.0 alpha:1];
    self.isGTE = YES;
    self.cellArray = @[@[@"钱包",@"订单管理",@"会员互转",@"我的算码"],@[@"icon_assets_m",@"icon_assets_o",@"icon_assets_h",@"icon_assets_sm"]];
    [self.view addSubview:self.tableView];

    
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.hidesBottomBarWhenPushed = NO;
    if (self.tabBarController.tabBar.hidden == YES) {
        self.tabBarController.tabBar.hidden = NO;
    }
    if (self.navigationController.navigationBar.hidden == NO) {
        self.navigationController.navigationBar.hidden = YES;
    }
    [self loadData];
    [self loginIfSuccess];
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginIfSuccess) name:Notification_login_ok_notice object:nil];
    }
    return self;
}

#pragma mark - load Data

-(void)loadData{
    ///加载GTE账户量
    [DKProgressHUD showLoading];
    [taskOverviewService getOverview:nil success:^(dataOverview *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            AssetsTableViewCell *cell = [self.tableView cellForRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:0]];
            cell.labelBiFreezeAmount.text = response.data.freezeAmount;
            cell.labelBiavailableAmount.text = response.data.availableAmount;
        }else{
            [DKProgressHUD showErrorWithStatus:response.statusDesc];
        }
        
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
    
    [taskUserManagerService getCommunity:nil success:^(dataCommunityOverview *response, BOOL success) {
        [DKProgressHUD dismiss];
        if (success) {
            self.headView.labelStartRZ.text = [NSString stringWithFormat:@"%@级 %@",response.data.level,response.data.status];
        }else{
            [DKProgressHUD showErrorWithStatus:response.statusDesc];
        }
    } failture:^(NSError *error, NSString *strDescription) {
        [DKProgressHUD dismiss];
        [DKProgressHUD showErrorWithStatus:strDescription];
    }];
}

-(void)loginIfSuccess
{
    self.headView.lbname.text = [UserCenter shareCenter].user.userName != nil ?  [UserCenter shareCenter].user.userName : @"GTE会员";
    self.headView.imageUrl.layer.masksToBounds = YES;
    self.headView.imageUrl.layer.borderWidth = 1.0f;
    self.headView.imageUrl.layer.borderColor = [UIColor clearColor].CGColor;
    self.headView.imageUrl.layer.cornerRadius = 30.0;
    [self.headView.imageUrl sd_setImageWithURL:[NSURL URLWithString:[UserCenter shareCenter].user.imageUrl] placeholderImage:[UIImage imageNamed:@"icon_avatar"]];
    dispatch_queue_t queue = dispatch_queue_create("net.bujige.Queue", DISPATCH_QUEUE_SERIAL);
    
    dispatch_sync(queue, ^{
        [taskOverviewService getOverview:nil success:^(dataOverview *response, BOOL success) {
            if (success) {
                self.gteInfo = response.data;
                self.gteInfo.dataName = @"GTE";
            }
        } failture:^(NSError *error, NSString *strDescription) {
            
        }];
    });
    
    dispatch_sync(queue, ^{
        [taskOverviewService getOverviewGTK:nil success:^(dataOverview *response, BOOL success) {
            if (success) {
                self.gtkInfo = response.data;
                self.gtkInfo.dataName = @"GTK";
                [self loadheadView];
            }
        } failture:^(NSError *error, NSString *strDescription) {
            
        }];
    });
}

-(void)loadheadView{
    NSMutableArray *array = [NSMutableArray new];
    if (self.gteInfo != nil && self.gtkInfo != nil) {
        [array addObject:self.gteInfo];
        [array addObject:self.gtkInfo];
        [self.headView.scrollView setDataInfoArray:array];
    }
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    NSArray <NSIndexPath *> *indexPathArray = @[indexPath];
    [self.tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationNone];
}


#pragma mark - loadtableview

-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-49) style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor clearColor];
        _tableView.userInteractionEnabled = YES;
    }
    return _tableView;
}

-(AssetsHeadView *)headView{
    if (!_headView) {
        _headView = [AssetsHeadView getHeadView];
        _headView.scrollView.delegate = self;
        [_headView.setButton addTarget:self action:@selector(setAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headView;
}

-(AssetsTopView *)topView{
    if (!_topView) {
        _topView = [AssetsTopView getTopView];
    }
    return _topView;
}

#pragma mark - delegate
-(void)didAssetsScrollWithGTE:(BOOL)isGTE
{
    self.isGTE = isGTE;
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
    NSArray <NSIndexPath *> *indexPathArray = @[indexPath];
    [self.tableView reloadRowsAtIndexPaths:indexPathArray withRowAnimation:UITableViewRowAnimationNone];
}


#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        return 1;
    }
    NSArray * array = self.cellArray.firstObject;
    return [array count];
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    if (section != 0) {
        UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 40)];
        footerView.backgroundColor = COLOR_Background;
        UIView *roundView = [[UIView alloc]initWithFrame:CGRectMake(15, 0, KScreenWidth - 30, 20)];
        roundView.backgroundColor = [UIColor whiteColor];
        UIBezierPath * maskPath = [UIBezierPath bezierPathWithRoundedRect:roundView.bounds byRoundingCorners:UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(10, 10)];
        CAShapeLayer * maskLayer = [[CAShapeLayer alloc]init];
        maskLayer.frame = roundView.bounds;
        maskLayer.path = maskPath.CGPath;
        roundView.layer.mask = maskLayer;
        [footerView addSubview:roundView];
        return footerView;
    }
    return nil;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 0) {
        return 0.0001;
    }
    return 40;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return 396;
    }
    return 60;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        return self.headView;
    }
    return self.topView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        return 220;
    }
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        AssetsTableViewCell *cell = [AssetsTableViewCell cellWithTableView:tableView];
        [cell setDataInfo:self.isGTE ? self.gteInfo : self.gtkInfo];
        return cell;
    }
    AssetsListTableViewCell *cell = [AssetsListTableViewCell cellWithTableView:tableView];
    cell.lbtitle.text = self.cellArray[0][indexPath.row];
    cell.iamgeView.image = [UIImage imageNamed:self.cellArray[1][indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    if (indexPath.section != 0) {
        switch (indexPath.row) {
            case 0:{
                QianBaoViewController *vc = [QianBaoViewController new];
                vc.isGTE = YES;
                [self.navigationController pushViewController:vc animated:YES];
                break;
            }
            case 1:
                [self.navigationController pushViewController:[AssetsOrderListViewController new] animated:YES];
                break;
            case 2:{
                QianBaoTiViewController *vc = [[QianBaoTiViewController alloc]initWithNibName:@"QianBaoTiViewController" bundle:nil];
                vc.isGTE = self.isGTE;
                [self.navigationController pushViewController:vc animated:YES];
            }
                break;
            case 3:{
                [self.navigationController pushViewController:[SuanMaViewController new] animated:YES];
            }
                break;
            default:
                break;
        }
    }else{
        [self.navigationController pushViewController:[[MeSourceDetailViewController alloc]initWithIsGTE:!self.isGTE] animated:YES];
    }
}


#pragma mark - event
-(void)setAction
{
    [self.navigationController pushViewController:[MeSetViewController new] animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
